<template>
  <div>商品类型

 <!-- 添加 -->
    <el-card class="box-card" style="margin:10px">
      <div>
        <span class="el-icon-goods"></span>
        <span> 商品类型数据列表</span>
        <el-button
          style="float: right;"
          type="button"
          size="small"
          @click="goAddPage"
          >添加</el-button
        >
      </div>
    </el-card>

      <el-table :data="productAttrList" border style="width: 100%;margin:10px">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="类型名称"></el-table-column>
      <el-table-column prop="attribute_count" label="属性数量"> </el-table-column>
      <el-table-column prop="param_count" label="参数数量"> </el-table-column>
       <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="open">编辑</el-button>
          <el-button type="primary" size="mini" class="btn" @click="delFun(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 1.分页 
    current-page 当前页码
    page-sizes   每页有多少条数据的下拉列表
    page-size    当前的列表每页有多少条数据
    total        数据的总数量
    size-change   修改每页有多少条数据
    current-change 修改当前是第几页

    4.分页：修改了current-page、page-sizes page-size total属性绑定的值
    -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
       style="float: right"
    >
    </el-pagination>
  </div>
</template>

<script>
//引入

import { fetchProductAttrList,deleteProductAttr } from "@/api/product";
export default {
  name: "productAttr",
  data: function () {
    return {
      productAttrList: [],
       //2.分页：定义变量
      listQuery: {
        pageSize: 5, //一页5条数据
        pageNum: 1, //当前页
      },
      total: 0, //总数量
    };
  },
   methods: {
     open() {
        this.$prompt('', '编辑类型', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
          // inputErrorMessage: '邮箱格式不正确'
        }).then(({ value }) => {
          this.$message({
            type: 'success',
            message: '提交信息成功: ' + value
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
      },

    
     //删除
    delFun(id) {
      console.log("删除触发了", id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          deleteProductAttr({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getProductData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    async getAttrList() {
      const res = await fetchProductAttrList(this.listQuery);
      console.log(res.data);
      this.productAttrList = res.data;
      //3.分页：获取数据的总量
      this.total = res.total;
    },
    handleSizeChange(val) {
        console.log("修改每页显示的条数 触发了",val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
      //重新获取数据
      this.getAttrList();
      },
      //6.分页：修改当前页的页码
      handleCurrentChange(val) {
        console.log("修改当前页的页码 触发了", val);
         this.listQuery.pageNum = val;
      this.getAttrList();
      }

  },
   mounted() {
    this.getAttrList();
  },
};

</script>

<style>
</style>
